<template>
	<mainContainer>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:sysdict:add'" @click="add">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增
				</el-button>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="refForm" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData" border>
			<el-table-column prop="type" label="字典类型" />
			<el-table-column prop="description" label="字典描述" />
			<el-table-column prop="remarks" label="备注" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<el-tag v-if="scope.row.status == '0'" type="danger">停用</el-tag>
					<el-tag v-if="scope.row.status == '1'" type="success">正常</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" width="260" align="center" fixed="right">
				<template #default="scope">
					<el-button link type="primary" @click="edit(scope.row)">
						<el-icon> <ele-Edit /> </el-icon>修改</el-button
					>
					<el-button link type="danger" @click="del(scope.row.id)"
						><el-icon> <ele-Delete /> </el-icon>删除</el-button
					>
					<el-button link type="primary" @click="(dictValueDrawer = true), (dictId = scope.row.id)"
						><el-icon> <ele-List /> </el-icon>键值列表</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<el-drawer size="40%" v-model="dictValueDrawer" direction="rtl">
			<template #header>
				<h4>键值列表</h4>
			</template>
			<template #default>
				<DictValue :dictId="dictId" v-if="dictValueDrawer" />
			</template>
		</el-drawer>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</mainContainer>
</template>
<script lang="ts">
import { defineComponent, defineAsyncComponent, ref, reactive, toRefs } from 'vue';
import { getPage, delObj } from '/@/api/upms/dict';
import { ElMessageBox, ElMessage } from 'element-plus';
export default defineComponent({
	components: { Form: defineAsyncComponent(() => import('./form.vue')), DictValue: defineAsyncComponent(() => import('../dictvalue/index.vue')) },
	setup() {
		const data = reactive({
			queryParams: {},
			page: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				ascs: [],
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);
		const refForm = ref();
		const dictId = ref('');
		const dictValueDrawer = ref(false);

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				ascs: data.page.ascs,
				descs: data.page.descs,
			};
			await getPage(Object.assign(params, data.queryParams))
				.then((response: any) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 新增按钮
		 */
		const add = () => {
			refForm.value.initForm();
		};
		/**
		 * 修改按钮
		 */
		const edit = (row: any) => {
			refForm.value.initForm(row);
		};
		/**
		 * 删除按钮
		 */
		const del = (id: string) => {
			ElMessageBox.confirm('此操作将删除该字典，是否继续?', '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delObj(id)
					.then((response: any) => {
						ElMessage.success('删除成功');
						initPage();
					})
					.catch(() => {});
			});
		};

		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = {};
		};
		initPage();
		return {
			dictId,
			dictValueDrawer,
			showSearch,
			loading,
			refForm,
			initPage,
			resetQuery,
			add,
			edit,
			del,
			...toRefs(data),
		};
	},
});
</script>
